package com.sg.service.base.dataassets.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.dataassets.req.QueryEviRecordDetailReq;
import com.sg.entity.OmsResourceEviRecord;
import com.sg.service.base.dataassets.MOmsResourceEviRecordService;
import com.wicket.okrdataassets.common.exception.BizException;
import com.wicket.okrdataassets.dal.mapper.mbg.OmsResourceEviRecordMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsResourceEviRecordServiceImpl
        extends ServiceImpl<OmsResourceEviRecordMapper, OmsResourceEviRecord>
        implements MOmsResourceEviRecordService {

    @Autowired
    OmsResourceEviRecordMapper omsResourceEviRecordMapper;

    /**
     * 8-01-02-新增存证记录[10303]
     * gen by moon at 11/11/2024, 1:24:43 AM
     */
    @Trace(operationName = "8-01-02-新增存证记录")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addEviRecord(OmsResourceEviRecord omsResourceEviRecord) {
        omsResourceEviRecord.setEviRecordId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsResourceEviRecord.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsResourceEviRecord.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsResourceEviRecord.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsResourceEviRecord.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsResourceEviRecord)) {
            return omsResourceEviRecord.getEviRecordId();
        } else {
            return "-1";
        }

    }

    /**
     * 8-01-02查询存证记录详情[10304]
     * gen by moon at 11/11/2024, 1:24:46 AM
     */
    @Trace(operationName = "8-01-02查询存证记录详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsResourceEviRecord queryEviRecordDetail(QueryEviRecordDetailReq reqDto) {

        try {
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            QueryWrapper<OmsResourceEviRecord> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsResourceEviRecord> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsResourceEviRecord retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
